﻿using System;
using System.IO;
using System.Windows.Forms;

namespace HYCodeBase.IO
{
	public class FileUtilityMethod
	{
		public static byte[] ReadFile(string fileName)
		{
			FileStream fileStream = null;
			byte[] array = new byte[0];
			byte[] result;
			try
			{
				fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
				BinaryReader binaryReader = new BinaryReader(fileStream);
				binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				array = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
				result = array;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
				result = array;
			}
			finally
			{
				if (fileStream != null)
				{
					fileStream.Close();
				}
			}
			return result;
		}

		public static bool WriteFile(byte[] pReadByte, string fileName)
		{
			FileStream fileStream = null;
			try
			{
				fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
				fileStream.Write(pReadByte, 0, pReadByte.Length);
			}
			catch
			{
				return false;
			}
			finally
			{
				if (fileStream != null)
				{
					fileStream.Close();
				}
			}
			return true;
		}
	}
}
